import { betterAuth } from 'better-auth'
import { prismaAdapter } from 'better-auth/adapters/prisma'
import { genericOAuth } from "better-auth/plugins";
import prisma from '@/lib/prisma'

export const auth = betterAuth({
    database: prismaAdapter(prisma, {
        provider: 'mysql',
    }),
    emailAndPassword: {
        enabled: true,
    },
    plugins: [
        genericOAuth({
            config: [
                {
                    providerId: "gitee",
                    clientId: process.env.GITEE_CLIENT_ID!,
                    clientSecret: process.env.GITEE_CLIENT_SECRET!,
                    authorizationUrl: "https://gitee.com/oauth/authorize",
                    tokenUrl: "https://gitee.com/oauth/token",
                    userInfoUrl: "https://gitee.com/api/v5/user",
                    scopes: ["user_info", "emails"],
                    // 自定义用户信息映射，确保返回格式符合Better Auth预期
                    mapProfileToUser: async (profile) => {
                        return {
                            id: profile.id,
                            email: profile.email,
                            name: profile.name,
                            emailVerified: true,
                            avatar: profile.avatar_url,
                        };
                    },
                },
            ],
        }),
    ],
    socialProviders: {
        github: {
            clientId: process.env.GITHUB_CLIENT_ID as string,
            clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
        }
    },
})